//
//  NewsTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsTableView.h"
#import "NewsTableViewCell.h"
#import "NewsTableHeaderView.h"

#import "PhotoShowViewController.h"
#import "NewsViewController.h"
#import "NewsDetailViewController.h"

@implementation NewsTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        
        self.delegate = self;
        self.dataSource = self;
        
        self.backgroundColor = [UIColor clearColor];
        
        [self registerClass:[NewsTableViewCell class] forCellReuseIdentifier:kNewsTableViewCellId];
    }
    
    return self;
}

#pragma mark - 数据的set方法
- (void)setNewsModelArray:(NSArray *)newsModelArray {
    _newsModelArray = [newsModelArray copy];
    
    [self resetHeaderView];
    [self reloadData];
}

#pragma mark - 重新设置头部视图。获取到的数据里面，第一条作为头部视图的数据
- (void)resetHeaderView {
    NewsModel* headerModel = [[NewsModel alloc] initWithDataDic:_newsModelArray[0]];
    //新闻页面，表视图的头部视图
    NewsTableHeaderView* headerView = [[NewsTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 250)];
    headerView.newsModel = headerModel;
    self.tableHeaderView = headerView;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _newsModelArray.count-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsTableViewCell* cell = [self dequeueReusableCellWithIdentifier:kNewsTableViewCellId forIndexPath:indexPath];
    
    NewsModel* model = [[NewsModel alloc] initWithDataDic:_newsModelArray[indexPath.row+1]];
    cell.newsModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel* model = [[NewsModel alloc] initWithDataDic:_newsModelArray[indexPath.row+1]];
    NSString* content = model.content;
    
    if (content.length) {          //新闻
        return 80;
    } else {                //图集
        return 120;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //通过响应者链,找到newsVC
    NewsViewController* newsVC = [Tools className:@"NewsViewController" fromResponder:self.nextResponder];
    
    if (newsVC) {
        NewsModel* model = [[NewsModel alloc] initWithDataDic:_newsModelArray[indexPath.row+1]];
        
        NSString* photoSetId = model.photosetID;
        if (photoSetId.length) {            //是图集
            PhotoShowViewController* photoShowVC = [[PhotoShowViewController alloc] init];
            photoShowVC.photoSetId = photoSetId;
            [newsVC.navigationController pushViewController:photoShowVC animated:YES];
            
        } else if (model.newsId.length) {                        //是新闻
            NewsDetailViewController* newsDetailVC = [[NewsDetailViewController alloc] init];
            newsDetailVC.newsId = model.newsId;
            [newsVC.navigationController pushViewController:newsDetailVC animated:YES];
        }
    }
}


@end
